from django import forms
from django.shortcuts import render
from zxy.models import UserInfo


class RegForm(forms.Form):
    name = forms.CharField(label='用户名')

    def clean_name(self):
        name = self.cleaned_data['name']
        if len(name) < 6:
            raise forms.ValidationError("你注册的用户名字符太短了")
        # 一定要返回，不然值变为None
        return name


# forms.py编写
class UserModelForm(forms.ModelForm):
    class Meta:
        model = UserInfo
        fields = "__all__"
        # widgets = {'password': widgets.PasswordInput()}
        labels = {
            'username': '姓名',
            'password': 'mima',
        }
        help_texts = {
            'username': '请输入用户名'
        }

    def clean_username(self):
        username = self.cleaned_data['username']
        if len(username) < 3:
            raise forms.ValidationError("你注册的用户名太短了！")
        return username
